package screenshot_demo;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class screenshot_demo {

	static final Logger logger = LogManager.getLogger(screenshot_demo.class
			.getName());
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();

	@BeforeMethod
	public void setUp() throws Exception {
		System.setProperty("webdriver.chrome.driver",
				"D:/selenium/selenium drivers/chromedriver.exe");
		driver = new ChromeDriver();
		baseUrl = "http://www.calculator.net/";
		// Puts a Implicit wait, Will wait for 10 seconds before throwing
		// exception
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		// Maximize the browser
		// driver.manage().window().maximize();

	}

	@Test
	public void main() throws Exception{
		DOMConfigurator.configure("log4j.xml");
		logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
		logger.info("TEST Has Started");

		// Launch website
		driver.navigate().to(baseUrl);
		logger.info("Open Calc Application");
		AutoScreenShot.ScreenShot(driver, "D:\\selenium\\screenshot\\");

		// Click on Math Calculators
		driver.findElement(
				By.xpath(".//*[@id='homelistdiv']/table/tbody/tr/td[3]/div[2]/a"))
				.click();
		logger.info("Clicked Math Calculator Link");

		// Click on Percent Calculators
		driver.findElement(By.xpath(".//*[@id='content']/ul[1]/li[3]/a"))
				.click();
		logger.info("Clicked Percent Calculator Link");

		// Enter value 10 in the first number of the percent Calculator
		driver.findElement(By.id("cpar1")).sendKeys("10");
		logger.info("Entered Value into First Text Box");

		// Enter value 50 in the second number of the percent Calculator
		driver.findElement(By.id("cpar2")).sendKeys("50");
		logger.info("Entered Value into Second Text Box");

		// Click Calculate Button
		driver.findElement(
				By.xpath(".//*[@id='content']/table[1]/tbody/tr[2]/td/input[2]"))
				.click();
		logger.info("Click Calculate Button");

		// Get the Result Text based on its xpath
		String result = driver.findElement(
				By.xpath(".//*[@id='content']/p[2]/font/b")).getText();
		logger.info("Get Text Value");
		
		BaseClass baseclass = new BaseClass(verificationErrors,driver);  
		baseclass.assertEqualsReWrite("6",result);
		

		// Print a Log In message to the screen
		logger.info(" The Result is " + result);

		if (result.equals("5")) {
			logger.info("The Result is Pass");

		} else {
			logger.error("TEST FAILED. NEEDS INVESTIGATION");

		}

		logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");

	}

	@AfterMethod
	public void tearDown() throws Exception {
		driver.quit();
		
		
	}

}